package com.oyjd.beauty.fw.util;

import java.io.File;
import java.io.StringReader;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * xml读取和解析
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class XmlUtil {

	/**
	 * 将文本xml转化为Document对象
	 * 
	 * @param sou
	 * @return
	 * @throws DocumentException
	 */
	public static Document parseTxtToDoc(String sou) throws DocumentException {
		SAXReader saxReader = new SAXReader();
		return saxReader.read(new StringReader(sou));
	}

	/**
	 * 将xml文件转化为Document对象
	 * 
	 * @param sou
	 * @return
	 * @throws DocumentException
	 */
	public static Document parseFileToDoc(String path) throws DocumentException {
		SAXReader saxReader = new SAXReader();
		return saxReader.read(new File(path));
	}

	@SuppressWarnings({ "unused", "unchecked" })
	public static void main(String[] args) throws DocumentException {
		//注意：xpath://xxx 即使是某个节点selectNodes(//xxx)也是获取整个文档的满足//xxx的所有节点
		//     要取某个节点下的 需要  .//xxx
		String EXPRESS_WORK_READ = "//object/property[@name='childDoc']/list/object";
		String EXPRESS_DBNAME = "property[@name='extend']/list/object[@name='extend']/property[@name='dbname']";
		String EXPRESS_DOC_BODY = "//object[@name='workBody']/property[@name='body']";
		String EXPRESS_MAIL = "//mailList/mailDoc";
		Document doc = parseFileToDoc("");

		String one = doc.selectSingleNode(EXPRESS_WORK_READ).getText();// 取节点值

		List<Element> list = doc.selectNodes(EXPRESS_MAIL);// 取多重复节点
		for (Element e : list) {
			String a = e.elementText("mail");// 取子节点值
			String b = e.elementText("/mail/property[@name='title']");// 取子节某属性的值
		}
	}

}
